// pages/carpay/carpay.js
var api = require('../../api.js');
var seti = null;
var overtime = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    minute:"00",
    second:"00",
    timeStamp:"",
    nonceStr: "123",
    wxpackage: "",
    paySign: "",
    appId:"",
    orderInfo:{},
    producttype:[
      { name: "未选择" },
      { name: "接机" },
      { name: "送机" },
      { name: "接站" },
      { name: "送站" },
      { name: "日租车" },
      { name: "大巴车" }
      ]
  },  
 
  nextpage: function () {
    wx.navigateTo({
      url: '../carpaysuccess/carpaysuccess',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var orderInfo = wx.getStorageSync('orderInfo');
    //console.log(orderInfo);
    this.setData({
      orderInfo: orderInfo
    })
    console.log("options.id:" + options.id);
    var that = this;
    wx.request({
      url: api.pay_unifiedorder,
      method: 'POST',
      data: {
        openId: orderInfo.openid,
        id: options.id
      },
      success: function (res) {
        console.log("wxpay");
        console.log(res.data);
        that.data.nonceStr = res.data.nonce_str;
        that.data.wxpackage = "prepay_id=" + res.data.prepay_id;
        console.log(that);
        wx.request({
          url: api.orderinfo,
          method: 'POST',
          data: {
            id: options.id,
          },
          success: function (res) {
            console.log(res.data);
            if (res.data.code==1)
            {
              wx.showToast({
                title: res.data.msg,
                icon: "none",
                duration: 2000
              })
              return false;
            }
            that.setData({
              orderinfo: res.data,
            })
            
            seti = setInterval(function () {
              var timestamp = Date.parse(new Date()) / 1000;
              overtime = res.data.payovertime - timestamp;
              console.log(overtime);
              if (overtime <= 0) {
                clearInterval(seti);
                return false;
              }
              that.setData({
                overtime: overtime,
                minute: Math.floor(overtime / 60),
                second: overtime % 60,
              })
            }, 1000)

          },
          fail: function (data) {
            console.log("orderinfo" + data);
          }
        })


      },
      fail: function (data) {
        console.log("wxpayfail" + data);
      }
    })
  },
  wxpaycallback: function () {
    if(overtime<0){
      wx.showToast({
        title: '支付已过期',
        icon:"none",
        duration: 2000
      })
      return false;
    }
    var that = this;
    wx.request({
      url: api.sign,
      method: 'POST',
      data: {
        nonceStr: that.data.nonceStr,
        package: that.data.wxpackage
      },
      success: function (res) {
        console.log(res.data);
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          'paySign': res.data.paySign,
          'success': function (res) {
            console.log(res);
            wx.redirectTo({
              url: '../carpaysuccess/carpaysuccess',
            })
          },
          'fail': function (res) {
            console.log(res);
          }
        })

      },
      fail: function (data) {
        console.log("isnew" + data);
      }
    })
    
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },
  
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(seti);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})